#!/usr/bin/env python
# _*_ coding:utf-8 _*_

from . import db
from flask import Flask

app = Flask(__name__)


# 会员数据模型
class User(db.Model):
    __tablename__ = "home"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    username = db.Column(db.String(100), unique=True)  # 用户名
    password = db.Column(db.String(100))  # 密码
    email = db.Column(db.String(100))  # 邮箱
    manufacturer = db.Column(db.String(100))  # 厂商名称

    def __repr__(self):
        return '<User %r>' % self.username

    def check_password(self, password):
        """
        检测密码是否正确
        :param password: 密码
        :return: 返回布尔值
        """
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password, password)


# 管理员
class Admin(db.Model):
    __tablename__ = "admin"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    username = db.Column(db.String(100), unique=True)  # 管理员账号
    password = db.Column(db.String(100))  # 管理员密码

    def __repr__(self):
        return "<Admin %r>" % self.username

    def check_password(self, password):
        """
        检测密码是否正确
        :param password: 密码
        :return: 返回布尔值
        """
        from werkzeug.security import check_password_hash
        return check_password_hash(self.password, password)


# 厂商模型
class Manufacturer(db.Model):
    __tablename__ = "manufacturer"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    uuid = db.Column(db.String(100), unique=True)  # 唯一标识
    manufacturer_name = db.Column(db.String(100), unique=True)  # 厂商名

    # hardware = db.relationship("Hardware", backref='manufacturer')  # 外键关系关联

    def __repr__(self):
        return "<manufacturer_name %r>" % self.manufacturer_name


# 硬件模型
class Hardware(db.Model):
    __tablename__ = "hardware"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    hardware = db.Column(db.String(100))  # 硬件设备
    # user_id = db.Column(db.Integer, db.ForeignKey('home.id'))  # 所属用户id
    manu_id = db.Column(db.Integer, db.ForeignKey('manufacturer.id'))  # 所属厂商id
    testLog = db.relationship("TestLog", backref='hardware')  # 外键关系关联
    testReview = db.relationship("TestReview", backref='hardware')  # 外键关系关联

    def __repr__(self):
        return "<hardware_name %r>" % self.hardware


# 日志
class TestLog(db.Model):
    __tablename__ = "testlog"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    task_id = db.Column(db.String(50))  # taskid
    job_name = db.Column(db.String(100))  # 任务名称
    upload_time = db.Column(db.DateTime)  # 上传时间
    hardware_id = db.Column(db.Integer,
                            db.ForeignKey('hardware.id'))  # 所属硬件设备id
    # testreview = db.relationship("TestReview", backref='TestLog', lazy=True,
    #                           cascade="all, delete-orphan")

    def __repr__(self):
        return "<TestLog %r>" % self.job_name


# 日志评审
class TestReview(db.Model):
    __tablename__ = "testreview"
    id = db.Column(db.Integer, primary_key=True)  # 编号
    testcase_name = db.Column(db.String(100))  # 测试用例
    test_result = db.Column(db.String(20))  # 测试结果
    review_result = db.Column(db.String(10), default="None")  # 评审结果
    review_comments = db.Column(db.String(255))  # 评审意见
    create_time = db.Column(db.DateTime)  # 评审时间
    log = db.Column(db.String(100))  # 评审结果所依据的日志链接
    taskid = db.Column(db.String(10))  # 评审结果所属的taskid

    menu_id = db.Column(db.Integer, db.ForeignKey('manufacturer.id'))  # 所属厂商
    hardware_id = db.Column(db.Integer,
                            db.ForeignKey('hardware.id'))  # 所属硬件设备id
    job_id = db.Column(db.Integer, db.ForeignKey('testlog.id'))  # 所属job

    def __repr__(self):
        return "<TestReview %r>" % self.testcase_name
